package com.changfeng.controller;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.dto.vo.AddressListVO;
import com.changfeng.bean.dto.vo.AddressSaveVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @PostMapping("/save")
    public void saveAddress(@RequestBody AddressSaveVO addressSaveVO) {
        addressService.SaveAddress(addressSaveVO);
    }

    @GetMapping("/getAddressList")
    public List<AddressListVO> getAddressList() {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        if (userAuth == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        return addressService.getAddressList(userAuth.getUserId());

    }

    @GetMapping("/getAddress")
    public AddressSaveVO getAddress(Integer addressId) {
        if (addressId == null) {
            throw  new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        return addressService.getAddress(addressId);
    }

    @GetMapping("/deleteAddress")
    public void deleteAddress(Integer addressId) {
        if (addressId == null) {
            throw  new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        addressService.deleteAddress(addressId);
    }

}
